<?php

namespace backend\controllers;

use Yii;
use yii\data\Pagination;
use backend\models\AdminLog;
use yii\web\NotFoundHttpException;

/**
 * AdminLogController implements the CRUD actions for AdminLog model.
 */
class AdminLogController extends BaseController
{
    public $layout = "lte_main";

    /**
     * Lists all AdminLog models.
     * @return mixed
     */
    public function actionIndex()
    {
        $query = AdminLog::find();
        $querys = Yii::$app->request->get('query');
        if (count($querys) > 0) {
            $condition = "";
            $parame = array();
            foreach ($querys as $key => $value) {
                $value = trim($value);
                if (empty($value) == false) {
                    $parame[":{$key}"] = $value;
                    if (empty($condition) == true) {
                        $condition = " {$key}=:{$key} ";
                    } else {
                        $condition = $condition . " AND {$key}=:{$key} ";
                    }
                }
            }
            if (count($parame) > 0) {
                $query = $query->where($condition, $parame);
            }
        }

        $pagination = new Pagination([
                'totalCount' => $query->count(),
                'pageSize' => '10',
                'pageParam' => 'page',
                'pageSizeParam' => 'per-page']
        );

        $orderby = Yii::$app->request->get('orderby', '');
        if (empty($orderby) == false) {
            $query = $query->orderBy($orderby);
        }


        $models = $query
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        return $this->render('index', [
            'models' => $models,
            'pages' => $pagination,
            'query' => $querys,
        ]);
    }

    /**
     * Displays a single AdminLog model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        //$id = Yii::$app->request->post('id');
        $model = $this->findModel($id);
        echo json_encode($model->getAttributes());

    }

    /**
     * Creates a new AdminLog model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new AdminLog();
        if ($model->load(Yii::$app->request->post())) {

            $model->create_user = Yii::$app->user->identity->uname;
            $model->create_date = date('Y-m-d H:i:s');

            if ($model->validate() == true && $model->save()) {
                $msg = array('errno' => 0, 'msg' => '保存成功');
                echo json_encode($msg);
            } else {
                $msg = array('errno' => 2, 'data' => $model->getErrors());
                echo json_encode($msg);
            }
        } else {
            $msg = array('errno' => 2, 'msg' => '数据出错');
            echo json_encode($msg);
        }
    }

    /**
     * Updates an existing AdminLog model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate()
    {
        $id = Yii::$app->request->post('id');
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post())) {


            if ($model->validate() == true && $model->save()) {
                $msg = array('errno' => 0, 'msg' => '保存成功');
                echo json_encode($msg);
            } else {
                $msg = array('errno' => 2, 'data' => $model->getErrors());
                echo json_encode($msg);
            }
        } else {
            $msg = array('errno' => 2, 'msg' => '数据出错');
            echo json_encode($msg);
        }

    }

    /**
     * Deletes an existing AdminLog model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete(array $ids)
    {
        if (count($ids) > 0) {
            $c = AdminLog::deleteAll(['in', 'id', $ids]);
            echo json_encode(array('errno' => 0, 'data' => $c, 'msg' => json_encode($ids)));
        } else {
            echo json_encode(array('errno' => 2, 'msg' => ''));
        }


    }

    /**
     * Finds the AdminLog model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return AdminLog the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = AdminLog::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
